﻿--[[
   @brief    craete group dialog
   @author   rickonzhang
   @version  1.0
   @date     2015-06-23
--]]

create_group_control = 
{
    invite_id_list = {};

    USER_INVITE_MAX_COUNT = 20;

--[[
   discussion_create_type 
   0 表示从主面板打开的讨论组创建界面
   1 表示从个人聊天窗口打开的讨论组创建界面
   2 表示从讨论组打开的讨论组邀请界面
--]]
    discussion_create_type = 0;

    discussion_init_members = ""; -- 传进来的人员，这些人员在界面不能移除的

    discussion_init_group_id = ""; -- 邀请用户进来的讨论组ID，创建的时候，这个值为空字符串

    init = function ()
        base_utility.log("[create_group_control] init");
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:AddLuaListener(ierd_tgp.social_network.SOCIAL_NETWORK_EVENT_CREATE_GROUP, 
            "create_group_control.create_group_rsp");
    end;

    show_create_group_dialog = function (event_id, json_str)
        ierd_tgp.tpf_ui.Require("social\\create_group\\view.lua");
        create_group_view:show(true);
        create_group_view:set_Title_text(LoadStr("SOCIAL_GROUP_CREATE"));
        local social =
            base_utility.get_component_adapter("ISocial_network",
                "ierd_tgp.social_network.Get_social_network_adapter();");
        local webkit_switch = social:get_webkit_switch();

        local url_str = "";
        if webkit_switch == 1 then
            url_str = LoadStr("SOCIA_CREATE_GROUP_URL");
        else
            url_str = LoadStr("SOCIA_CREATE_GROUP_VER1_URL");
        end

        create_group_view:update_url(web_utility.make_login_status_url(url_str, login_model.main_uin));
    end;

    on_js_callback = function(uilib, func_name, json_str)
        base_utility.log("[create_group_control]on_js_callback, func_name:"..func_name);
        if func_name == "create_group" then
            create_group_control.create_group(json_str);
        elseif func_name == "close" then
            create_group_view:close();
        elseif func_name == "get_qq_friend_list" then
            create_group_control.get_qq_friend();
        elseif func_name == "get_tgp_friend_list" then
            create_group_control.get_tgp_friend();
        elseif func_name == "get_my_info" then
            create_group_control.get_my_info();
        elseif func_name == "create_discussion_group" then
            create_group_control.create_discussion_group(json_str);
        elseif func_name == "show_talk_dialog" then
            create_group_control.show_talk_dialog(json_str);
        elseif func_name == "get_discussion_create_info" then 
            create_group_control.get_discussion_create_info();
        elseif func_name == "invite_friend" then
            create_group_control.invite_friend(json_str);
        end
    end;

    show_talk_dialog = function(json_str)
        local group_info = base_utility.json_to_table(json_str);
        if group_info.group_id == nil then
            return;
        end

        local table_param = {};
        local groups = {};

        local info = {};
        info.id = group_info.group_id;
        info.is_new = false;
        ierd_tgp.tpf_ui.Require("social\\group_list\\model.lua");
        info.time = group_list_model.get_last_msg_time(info.id);
        info.group_type = group_list_model.get_group_type(info.id);
        table.insert(groups, info);
        
        table_param["groups"] = groups;

        event_center.send_event(group_event.SHOW_TALK_DIALOG,base_utility.table_to_json(table_param));
    end;

    get_my_info = function ()
        local my_info = {};
        my_info["id"] = login_model.im_login_infomation["friend_id"];
        my_info["my_nick"] = login_model.im_login_infomation["name"];
        my_info["head_url"] = login_model.im_login_infomation["url"];

        local  call_param = {};
        call_param["retCode"] = 0;
        call_param["msg"] = "success";
        call_param["data"] = my_info;
       web_utility.lua_call_js("get_my_info",base_utility.table_to_json(call_param), CID_WEB_CTRL_FOR_CREATE_GROUP);
    end;


    create_group = function(json_str)
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");

        --[[
        typedef struct tagGroupInfo {
             char szGroup_id[COMM_MAX_IM_NAME_LEN];                 /*   群id */
            char szGroup_name[COMM_MAX_IM_NAME_LEN];                /*   群名称 */
            char szGroup_owner[COMM_MAX_IM_NAME_LEN];               /*   群主 */
            char szGroup_introduce[COMM_MAX_COMMSTR_LEN];       /*   群简介 */
            char szGroup_notifycation[COMM_MAX_COMMSTR_LEN];    /*   群公告 */
            char szGroup_face_url[COMM_MAX_COMMSTR_LEN];        /*   群头像地址 */
            char szGroup_type[COMM_MAX_IM_NAME_LEN];                /*   Private;Public;ChatRoom */
            char szExData1[COMM_MAX_COMMSTR_LEN];               /*   群扩展字段1 */
            char szExData2[COMM_MAX_COMMSTR_LEN];               /*   扩展字段2 */
            unsigned int dwGroup_member_num;                        /*   群成员数量 */
            unsigned int dwGroup_max_member_num;                    /*   群最大成员数量 */
            unsigned int dwGroup_create_time;                       /*   创建时间 */
            unsigned int dwGroup_last_msg_time;                     /*   最近消息时间 */
            unsigned int dwMsgFlag;                                         // 消息屏蔽选项（0：接收；1，拒绝）. 最小权限: ROOT    自己可以修改
            unsigned int dwAdd_option;                                   // 申请加群选项：0禁止加入；1需要审批；2随意加入

            tagGroupInfo() {
                memset(this, 0, sizeof(tagGroupInfo));
                strncpy_s(szGroup_type
                    , "Public"
                    , sizeof(szGroup_type));
                dwAdd_option = 1;
            };

            ~tagGroupInfo() {};
        }
        
            新定义下格式
            {
                city:"xxx"
                address:"xxx"
                location:{"xxx", lat:"xxx"}
                game:"xxx"
                game_zone:"xxx"
                face_url:"xxx"
                name:"xxx"
                friends : {
                    from_tgp : {{game:"xx", uid:"xx"}},
                    from_qq : {},
                    from_game : {}
                }
            }

        ]]
        local group_info = ierd_tgp.social_network.tagGroupInfo:new();
        local param_tbl = base_utility.json_to_table(json_str);
        group_info.szGroup_name = param_tbl["name"];
        group_info.szGroup_owner = login_model.main_uin;
        group_info.szGroup_face_url = param_tbl["face_url"];
        group_info.dwGroup_max_member_num = 500;

        local ex_data1 = {};
        ex_data1["city"] = param_tbl["city"];
        ex_data1["addr"] = param_tbl["address"];
        ex_data1["loc"] = param_tbl["loc"]; -- 经度纬度 {param_tbl["loc"] = {lng:"xxx", lat:"xxx"} }
        ex_data1["province"] = param_tbl["province"];
        ex_data1["district"] = param_tbl["district"];
        local ex_data1_str =base_utility.table_to_json(ex_data1);
        group_info.szExData1 = ex_data1_str;

        local ex_data2 = {};
        if param_tbl["game"] ~= nil then
            ex_data2["game"] = param_tbl["game"];
        end
        if param_tbl["game_zone"] ~= nil then
            ex_data2["gzone"] = param_tbl["game_zone"];
        end
        local ex_data2_str = base_utility.table_to_json(ex_data2);
        group_info.szExData2 = ex_data2_str;

        local friend_list = {};
        if next(param_tbl["friends"]["from_tgp"]) then
            for i, v in pairs(param_tbl["friends"]["from_tgp"]) do
                table.insert(friend_list, v["tgp_id"]);
            end
        end
        if next(param_tbl["friends"]["from_game"]) then
            for i, v in pairs(param_tbl["friends"]["from_game"]) do
                table.insert(friend_list, v["tgp_id"]);
            end
        end
        if next(param_tbl["friends"]["from_qq"]) then
            for i, v in pairs(param_tbl["friends"]["from_qq"]) do
                table.insert(friend_list, v["tgp_id"]);
            end
        end

        -- 少于20人的群，直接拉进群
        local friend_list_str = "{}";
        if #friend_list == 0 then
            friend_list_str = "{}";
            create_group_control.invite_id_list = {};
        else
            if (#friend_list + 1) > create_group_control.USER_INVITE_MAX_COUNT then
                create_group_control.invite_id_list["id_list"] = friend_list;
                friend_list_str = "{}";
            else
                create_group_control.invite_id_list = {};
                friend_list_str =base_utility.table_to_json(friend_list);
            end
        end


        local social =
            base_utility.get_component_adapter("ISocial_network",
                "ierd_tgp.social_network.Get_social_network_adapter();");
        social:create_group(group_info, friend_list_str);
        group_info:delete();
    end;

    create_group_rsp = function(event_id, json_str)
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        --tpfui:RemoveLuaListener(ierd_tgp.social_network.SOCIAL_NETWORK_EVENT_CREATE_GROUP, "create_group_control.create_group_rsp");
        local param_tbl = base_utility.json_to_table(json_str);
        if param_tbl["result"] == 0 and next(create_group_control.invite_id_list) then
            local tpfui =
                base_utility.get_component_adapter("ITpf_ui",
                    "ierd_tgp.tpf_ui.GetTPFUI();");
            tpfui:AddLuaListener(ierd_tgp.social_network.SOCIAL_NETWORK_EVENT_INVITE_GROUP, "create_group_control.invite_friend_to_group_rsp");

            local g_id = param_tbl["group_id"];
            local social =
                base_utility.get_component_adapter("ISocial_network",
                    "ierd_tgp.social_network.Get_social_network_adapter();");
            social:invite_group(g_id,base_utility.table_to_json(create_group_control.invite_id_list));
            create_group_control.invite_id_list = {};
        end
        local rsp_data = {};
        rsp_data["retCode"] = param_tbl["result"];
        rsp_data["data"] = {};
        rsp_data["data"]["max_member_num"] = param_tbl["max_member_num"]; 
        rsp_data["data"]["member_num"] = param_tbl["member_num"];
        rsp_data["data"]["member_num"] = param_tbl["member_num"];
        rsp_data["data"]["group_id"] = param_tbl["group_id"];
        rsp_data["data"]["name"] = param_tbl["name"];
        rsp_data["data"]["owner"] = param_tbl["owner"];
        rsp_data["data"]["group_type"] = param_tbl["group_type"];
        rsp_data["data"]["ext_data1"] = param_tbl["ext_data1"];
        rsp_data["data"]["ext_data2"] = param_tbl["ext_data2"];
          
        --将结果传递给js
        web_utility.lua_call_js("create_discussion_group", base_utility.table_to_json(rsp_data),
            CID_WEB_CTRL_FOR_CREATE_GROUP);

        if param_tbl["result"] == 0 then
            local table_param = {};
            local info = {};
            info.id     = param_tbl["group_id"];
            info.is_new = false;
            info.time   = 0;
            info.group_type = param_tbl["group_type"];
            local groups = {};
            table.insert(groups, info);
            table_param["groups"] = groups;
            event_center.send_event(group_event.SHOW_TALK_DIALOG, base_utility.table_to_json(table_param));
        end
    end;

    invite_friend_to_group_rsp = function(event_id, json_str)
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:RemoveLuaListener(ierd_tgp.social_network.SOCIAL_NETWORK_EVENT_INVITE_GROUP, "create_group_control.invite_friend_to_group_rsp");

        local result_param = base_utility.json_to_table(json_str);
        if result_param["result"] ~= 0 then
            base_utility.log("[create_group_control]invite_friend , error:"..tostring(result_param["result"])..", group_id:"..result_param["group_id"], true);
        end
    end;

    get_qq_friend = function()
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:AddLuaListener(ierd_tgp.social_network.SOCIAL_NETWORK_EVENT_GET_QQ_FRIEND, "create_group_control.get_qq_friend_rsp");

        local social =
            base_utility.get_component_adapter("ISocial_network",
                "ierd_tgp.social_network.Get_social_network_adapter();");
        social:get_qq_friend();
    end;

    get_qq_friend_rsp = function(event_id, json_str)
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:RemoveLuaListener(ierd_tgp.social_network.SOCIAL_NETWORK_EVENT_GET_QQ_FRIEND, "create_group_control.get_qq_friend_rsp");

        local qq_friends = {};
        local qq_friend_tbl = base_utility.json_to_table(json_str);
        if qq_friend_tbl["result"] == 0 and qq_friend_tbl["friends"] then
            for i, friend in ipairs(qq_friend_tbl["friends"]) do
                local tmp = {};
                tmp["friend_id"] = friend["friend_id"];
                tmp["name"] = friend["new_name"] or "";
                if "" == tmp["name"] then
                    tmp["name"] = friend["name"];
                end
                tmp["sex"] = friend["sex"];
                tmp["face_url"] = friend["url"];
                table.insert(qq_friends, tmp);
            end
        end

        local  call_param = {};
        call_param["retCode"] = 0;
        call_param["msg"] = "success";
        call_param["data"] = qq_friends;
       web_utility.lua_call_js("get_qq_friend_list",base_utility.table_to_json(call_param), CID_WEB_CTRL_FOR_CREATE_GROUP);
    end;

    get_tgp_friend = function()
        ierd_tgp.tpf_ui.Require("social\\friend_list\\model.lua");
        local tgp_friend_tbl = friend_list_model.friend_table_ ;

        local tgp_friends = {};
        if next(tgp_friend_tbl) ~= nil then -- table不空
            for i, friend in ipairs(tgp_friend_tbl) do
                local tmp = {};
                tmp["friend_id"] = friend["friend_id"];
                tmp["name"] = friend["new_name"] or "";
                if "" == tmp["name"] then
                    tmp["name"] = friend["name"];
                end
                tmp["sex"] = friend["sex"];
                tmp["face_url"] = friend["url"];
                table.insert(tgp_friends, tmp);
            end
        end

        local  call_param = {};
        call_param["retCode"] = 0;
        call_param["msg"] = "success";
        call_param["data"] = tgp_friends;
       web_utility.lua_call_js("get_tgp_friend_list",base_utility.table_to_json(call_param), CID_WEB_CTRL_FOR_CREATE_GROUP);
    end;

    -- 显示讨论组创建窗口
    show_create_discussion_dailog = function(event_id, json_str)
        ierd_tgp.tpf_ui.Require("social\\create_group\\view.lua");
        create_group_view:show(true);
        create_group_view:set_Title_text(LoadStr("SOCIAL_GROUP_CREATE_DISCASS"));
        local url_param = "";
        local type_param = "";
        local group_id_param = "";
        create_group_control.discussion_create_type = 0;
        create_group_control.discussion_init_group_id = "";
        create_group_control.discussion_init_members = "";
        if json_str ~= nil then
            local param_table = base_utility.json_to_table(json_str);
            if param_table ~= nil and param_table["friend_id"] ~= nil then
                create_group_control.discussion_init_members = param_table["friend_id"];
            end
            if param_table ~= nil and param_table["type"] ~= nil then
                create_group_control.discussion_create_type = param_table["type"];
            end
            if param_table ~= nil and param_table["group_id"] ~= nil then
                create_group_control.discussion_init_group_id = param_table["group_id"];
            end
        end
        local url_str = LoadStr("SOCIA_CREATE_DISCUSS_URL");

        local url = web_utility.make_login_status_url(url_str, login_model.main_uin);
        create_group_view:update_url(url);
    end;

    create_discussion_group = function(json_str)
         --[[
            新定义下格式
            {
                name:"xxx"
                friends : {
                    [1] = "xx",[2] ="xx"
                }
            }

        ]]
        local group_info = ierd_tgp.social_network.tagGroupInfo:new();
        local param_tbl = base_utility.json_to_table(json_str);
        if nil == param_tbl then return end

        group_info.szGroup_name = param_tbl["name"];
        group_info.szGroup_owner = login_model.im_login_infomation["friend_id"];
        group_info.szGroup_face_url = param_tbl["face_url"];
        group_info.dwGroup_max_member_num = 50;
        group_info.szGroup_type = "MobileDiscus";   -- key data

        local friend_list = {};
        if _G.next(param_tbl["friends"]) then
            for i, v in pairs(param_tbl["friends"]) do
                table.insert(friend_list, tostring(v));
            end
        end

        local friend_list_str = "{}";
        if #friend_list == 0 then
            friend_list_str = "{}";
            create_group_control.invite_id_list = {};
        else
            create_group_control.invite_id_list = {};
            friend_list_str = base_utility.table_to_json(friend_list);
        end


        local social =
            base_utility.get_component_adapter("ISocial_network",
                "ierd_tgp.social_network.Get_social_network_adapter();");
        social:create_group(group_info, friend_list_str);
        group_info:delete();
    end;

    get_discussion_create_info = function()
        -- body
        local data = {};
        data["list"] = create_group_control.discussion_init_members;
        data["type"] = create_group_control.discussion_create_type;
        data["group_id"] = create_group_control.discussion_init_group_id;
        local  call_param = {};
        call_param["retCode"] = 0;
        call_param["msg"] = "success";
        call_param["data"] = data;
        web_utility.lua_call_js("get_discussion_create_info",base_utility.table_to_json(call_param), CID_WEB_CTRL_FOR_CREATE_GROUP);
    end;

    invite_friend = function(json_str)
        local param_tbl = base_utility.json_to_table(json_str);
        local group_id = "";
        local friend_list = {};
        friend_list["id_list"] = {};
        if param_tbl ~= nil and param_tbl["group_id"] ~= nil then
            group_id = param_tbl["group_id"];
        end
        if _G.next(param_tbl["friends"]) then
            for i, v in pairs(param_tbl["friends"]) do
                table.insert(friend_list["id_list"], tostring(v));
            end
        end
        local social =
            base_utility.get_component_adapter("ISocial_network",
                "ierd_tgp.social_network.Get_social_network_adapter();");
        social:invite_group(group_id,base_utility.table_to_json(friend_list));
    end;

    get_friendlist_done = function(event_id, json_str)
        local friend_table = base_utility.json_to_table(json_str);
        if friend_table.friends and #friend_table.friends > 0 then
            base_utility.log(string.format("[get_friendlist_done] friend count = %d", #friend_table.friends));
            local tgp_friend_tbl = friend_table.friends;
            local tgp_friends = {};
            if next(tgp_friend_tbl) ~= nil then -- table不空
                for i, friend in ipairs(tgp_friend_tbl) do
                    local tmp = {};
                    tmp["friend_id"] = friend["friend_id"];
                    tmp["name"] = friend["new_name"] or "";
                    if "" == tmp["name"] then
                        tmp["name"] = friend["name"];
                    end
                    tmp["sex"] = friend["sex"];
                    tmp["face_url"] = friend["url"];
                    table.insert(tgp_friends, tmp);
                end
            end

            local  call_param = {};
            call_param["retCode"] = 0;
            call_param["msg"] = "success";
            call_param["data"] = tgp_friends;
            web_utility.lua_call_js("get_tgp_friend_list",
                base_utility.table_to_json(call_param), CID_WEB_CTRL_FOR_CREATE_GROUP);
        end
    end;
}

create_group_control.init();